# config valid only for current version of Capistrano
lock "3.7.1"

set :application, "guike"
set :repo_url, "git@git.oschina.net:beiyiwangdege/guike.git"
set :scm, :git
set :use_sudo,false
set :keep_releases,5
set :branch,"master"
set :rails_env,:production
set :enable_ssl,false
set :bundle_bins, fetch(:bundle_bins, []).push('magic_encoding','unicorn_rails','rake','sidekiq')
set :log_level,:info
set :linked_dirs, %w{log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system }
namespace :deploy do
  
  desc "Update crontab with whenever"
  task :update_cron do
    on roles(:app) do
      within "#{fetch(:deploy_to)}/current" do
        execute :whenever,"--update-crontab #{fetch(:application)}"
      end
    end
  end

  desc "Update topic categories"
  task :update_topic_categories do
    on roles(:app) do
      within "#{fetch(:deploy_to)}/current" do
        execute :rake,"topic_category:update"
      end
    end
  end

  desc "start sidekiq"
  task :start_sidekiq do
    on roles(:app) do
      within "#{fetch(:deploy_to)}/current" do
        with RAILS_ENV: fetch(:rails_env) do
            # puts capture "kill -QUIT `cat #{deploy_to}/current/tmp/pids/sidekiq.pid`"
            # execute :kill,"-QUIT","`cat #{deploy_to}/current/tmp/pids/sidekiq.pid`"
            execute :sidekiq,"-i","2","-e","production","-d"
        end  
      end  
    end    
  end 


  desc "Stop unicorn"
  task :stop_unicorn do
    on roles(:app) do
      puts capture "kill -QUIT `cat #{deploy_to}/current/tmp/pids/cdgietf_unicorn.pid`"
    end
  end

  after :finishing, 'deploy:cleanup'
  # after :finishing, 'deploy:start_sidekiq'
  
end

